#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "./rmblast-2.10.0.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

if [[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]]; then echo "请输入正确的构建路径"; exit 1; fi
if [[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]]; then echo "请输入正确的安装路径"; exit 1; fi
[[ "$1" = "$2" ]] && echo "构建路径与安装路径不能相同" && exit 1
buildpath=$1
installpath=$2
if [[ "$1" =~ .*/$ ]]; then buildpath=${buildpath%/*}; fi
if [[ "$2" =~ .*/$ ]]; then installpath=${installpath%/*}; fi

yum install lmdb* -y
echo "/usr/local/lib" >> /etc/ld.so.conf
/sbin/ldconfig
wget -t 10 -c -P $buildpath https://ftp.ncbi.nlm.nih.gov/blast/executables/blast+/2.10.0/ncbi-blast-2.10.0+-src.tar.gz
wget -t 10 -c -P $buildpath http://www.repeatmasker.org/isb-2.10.0+-rmblast.patch.gz
cd $buildpath
tar xf ncbi-blast-2.10.0+-src.tar.gz
gunzip isb-2.10.0+-rmblast.patch.gz
cd ncbi-blast-2.10.0+-src
patch -p1 < ../isb-2.10.0+-rmblast.patch
cd c++
sed -i '/LIBS =/s/$/ -llmdb/' ./src/app/rmblastn/Makefile.rmblastn.app
./configure --with-mt --without-debug --without-krb5 --without-openssl --with-projects=scripts/projects/rmblastn/project.lst --prefix=$installpath/rmblast
make -j$(nproc) CC=clang CXX=clang++
make install
cat>"$installpath/rmblast/rmblast_modulefiles"<<EOF
#%Module1.0
conflict rmblast
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set rmblast \$pwd
setenv rmblast \$rmblast
prepend-path PATH \$rmblast/bin
EOF
